package com.TetrisII.views;


import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JSlider;

import com.TetrisII.control.ShapeControllers;

public class UserDialog extends JDialog implements ItemListener{

	private JSlider speedSlider;//进度条
    private JButton confirmButton;//确认按钮
    private ShapeControllers control;
    private Checkbox box_1,box_2,box_3;//三个等级复选框
    private JButton upButton,cancelButton;//上涨和取消上涨按钮
    private  CheckboxGroup levelBox;//三个等级复选框组
    public UserDialog(ShapeControllers control) {
	    this.control =control;
		JPanel dialogPanel=new JPanel();		
		this.setTitle("自定义面板");
		this.setSize(300, 250);
		//创建速度进度条
		speedSlider=new JSlider();	
		speedSlider.setMajorTickSpacing(20);//最大的值
		speedSlider.setMinorTickSpacing(10);//最小的值	
		speedSlider.setPaintTicks(true);
		speedSlider.setPaintLabels(true);		
		//确定按钮
		confirmButton=new JButton("确定");
		confirmButton.setSize(40, 50);
		confirmButton.addActionListener(new UserDialogListener());
	    dialogPanel.setLayout( new FlowLayout(FlowLayout.CENTER,30,25));
	    dialogPanel.add(speedSlider);		
		//三个等级框
	      levelBox = new CheckboxGroup();
	      box_1=new Checkbox("低级", levelBox,true);
		 box_2=new Checkbox("中级",levelBox,false);
		 box_3=new Checkbox("高级", levelBox,false);
		 box_1.addItemListener(this);
		 box_2.addItemListener(this);
		 box_3.addItemListener(this);
	     upButton=new JButton("底层上涨");
	     upButton.addActionListener(new UserDialogListener());
	     cancelButton=new JButton("取消上涨");
	     cancelButton.addActionListener(new UserDialogListener());
		 dialogPanel.add(box_1);
	     dialogPanel.add(box_2);
	     dialogPanel.add(box_3);
	     dialogPanel.add(upButton);
	     dialogPanel.add(cancelButton);
	     dialogPanel.add(confirmButton);	    
	    
		this.add(dialogPanel);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setModal(true);
		this.setVisible(true);
		
	}
    /**
     * 进度条事件，调节下降速度
     * @author Administrator
     *
     */
	class UserDialogListener implements ActionListener{
		
		
		@Override
		public void actionPerformed(ActionEvent e) {
		   int n= (speedSlider.getValue()+15)*15;
		   System.out.println(n);
		   control.setShapeSpeed(n);
			UserDialog.this.setVisible(false);
			
			
			if(e.getSource()==upButton){
				System.out.println("上涨");
				
				control.setBingo(true);
			}
			if (e.getSource()==cancelButton) {
				System.out.println("取消上涨");
				control.setBingo(false);
			}
		}
		
									
	}
	/**
	 * 复选框事件，选择等级
	 */
	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		
		if (e.getSource()==box_1) {
			
			control.setShapeLevel(1);
//			control.restartGame();
		}else if(e.getSource()==box_2){
		
			control.setShapeLevel(2);
//			control.restartGame();
		}else if (e.getSource()==box_3) {
			
			control.setShapeLevel(3);
//			control.restartGame();
		}
		
	
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
